﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace hlog {
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：FileCache                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2022/3/23 10:52:37                        
   *└────────────────────────────────────────────────┘
   **/
    public static class FileCache {

       private class FileData {
            public byte[] b;
            public long time = 0;
        }
        private static object obj = new object();

        private static int size = 0;
        public static int getCacheSize() {
            lock( obj ) {
                return size;
            }
        }

        static IMServer mServer ;
        private static Dictionary<string ,FileData> cacheData = new Dictionary<string, FileData>();

        public static byte[] loadFile(String filePath) {
            if( !File.Exists(filePath) ) {
                return new byte[0];
            }

            long fileTime = File.GetLastWriteTime(filePath).Ticks;
            if( cacheData.ContainsKey(filePath) ) {
                FileData fd = cacheData[filePath];
                if( fd.time == fileTime ) {
                    return fd.b;
                } else {
                    byte[] b = myLoad(filePath);

                    cacheData[filePath] = new FileData() { time = fileTime ,b = b };
                    return b;
                }

            } else {
                byte[] b = myLoad(filePath);

                cacheData.Add(filePath, new FileData() { time = fileTime ,b = b });
                return b;

            }

    
          
        }
        private static byte[] myLoad(String filePath) {
            lock( obj ) {
                FileStream fileStream = null;
                byte[] b = null;
                int length = 0;
                try {
                    fileStream = File.OpenRead(filePath);
                    length = ( int ) fileStream.Length;
                    b = new byte[length];
                    fileStream.Read(b ,0 ,length);
                } finally {
                    if( fileStream != null ) {
                        fileStream.Close();
                    }
                }
                size = length;
                foreach( var d in cacheData ) {
                    size += d.Value.b.Length;
                }

                String s = String.Format("load file {0} size {1}Byte all " ,filePath ,length );
                if( size > 1024 * 1024 * 10 ) {
                    s += size / 1024 / 1024 + "M";
                } else if( size > 1024 *10 ) {
                    s += size / 1024 + "K";
                } else {
                    s += size;
                }   
                if( mServer == null ) {
                    mServer = GuidangFactray.getDal<IMServer>();
                }
                     
                mServer.save(new MessageLog(s));

                return b;
            }
        }

    }
}
